<template>
	<a-drawer
		:title="formData.id ? '编辑外部单位' : '增加外部单位'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-spin :spinning="spinning">
			<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
				<a-form-item label="代码：" name="code">
					<a-input v-model:value.trim="formData.code" placeholder="请输入代码" allow-clear
							 :disabled="formData.id"/>
				</a-form-item>
				<a-form-item label="名称：" name="name">
					<a-input v-model:value.trim="formData.name" placeholder="请输入名称" allow-clear/>
				</a-form-item>
				<a-form-item label="单位地址：" name="address">
					<a-input v-model:value.trim="formData.address" placeholder="请输入单位地址" allow-clear/>
				</a-form-item>
				<a-form-item label="营业执照：" name="busLic">
					<a-upload
						v-model:file-list="formData.busLic"
						class="avatar-uploader"
						list-type="picture-card"
						:show-upload-list="false"
						:custom-request="customRequest"
						accept="image/png, image/jpeg, image/jpg"
					>
						<img v-if="imageUrl" :src="imageUrl" alt="avatar" style="max-height: 100px; max-width: 100px"/>
						<div v-else>
							<plus-outlined/>
							<div class="ant-upload-text">上传</div>
						</div>
					</a-upload>
				</a-form-item>
				<a-form-item label="排序：" name="sortCode">
					<a-slider v-model:value="formData.sortCode" :max="100"/>
				</a-form-item>
			</a-form>
		</a-spin>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="extOrgForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import { message } from "ant-design-vue";
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		sortCode: 0
	})
	const submitLoading = ref(false)
	const imageUrl = ref('')
	const spinning = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		spinning.value = true
		formData.value = {
			sortCode: 99
		}
		if (record) {
			bizOrgApi.orgDetail({
				id: record.id
			}).then((data) => {
				let recordData = cloneDeep(data)
				if (data.busLic) {
					//组件是个数组
					recordData.busLic = [data.busLic]
					// 让其回显
					imageUrl.value = data.busLic
				} else {
					recordData.busLic = []
				}
				formData.value = Object.assign({}, recordData)
			}).finally(() => {
				spinning.value = false
			})
		} else {
			spinning.value = false
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
		imageUrl.value = ''
	}
	// 默认要校验的
	const formRules = {
		code: [required('请输入代码')],
		name: [required('请输入名称')],
		sortCode: [required('请选择排序')]
	}

	const customRequest = (data) => {
		formData.value.busLic = ref([])
		getBase64(data.file)
			.then((res) => {
				imageUrl.value = res
				formData.value.busLic.push(res)
			})
			.catch((err) => {
			})
	}

	// 文件转base64，用于显示图片
	const getBase64 = (file) => {
		return new Promise((resolve, reject) => {
			// FileReader类就是专门用来读文件的
			const reader = new FileReader()
			reader.readAsDataURL(file)
			// 成功和失败返回对应的信息，reader.result一个base64，可以直接使用
			reader.onload = () => resolve(reader.result)
			// 失败返回失败的信息
			reader.onerror = (error) => reject(error)
		})
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formDataParam.busLic) {
				formDataParam.busLic = formDataParam.busLic[0]
			}
			formDataParam.orgType = 1
			bizOrgApi
				.submitForm(formDataParam, !formDataParam.id)
				.then(() => {
					if(formDataParam.id){
						message.success('编辑成功')
					}else{
						message.success('新增成功')
					}
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
